package com.cxq.finalproject;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.cxq.finalproject.databinding.ActivityLoginBinding;
import com.cxq.finalproject.util.MyUserHelper;

public class LoginActivity extends AppCompatActivity {
//    数据绑定一下
    private ActivityLoginBinding bing;
//    数据库操作类
    private MyUserHelper myUserHelper;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //初始化activityLoginBinding,绑定到R.layout.activity_login
        bing = DataBindingUtil.setContentView(this,R.layout.activity_login);
        // 打开数据表库表
        myUserHelper = new MyUserHelper(this, MyUserHelper.DB_NAME, null, 1);
        SQLiteDatabase db = myUserHelper.getWritableDatabase();
        myUserHelper.onCreate(db);
        // 获得Preferences
        sp = getSharedPreferences("Login_UserInfo", MODE_PRIVATE);
        if (sp.getString("isRem", "").equals("1")){
            bing.setUserId(sp.getString("userId",""));
            bing.setPassword(sp.getString("password",""));
            bing.checkBox.setChecked(true);
        }
    }

    public void login_click(View view){
        if (bing.getUserId() == null){
            Toast.makeText(getApplicationContext(), "用户名不能为空！",
                    Toast.LENGTH_SHORT).show();
        }else if(bing.getPassword() == null){
            Toast.makeText(getApplicationContext(), "密码不能为空！",
                    Toast.LENGTH_SHORT).show();
        }else if( isExist() ){
//            Intent intent = new Intent();
            // 获得Editor
            SharedPreferences.Editor editor = sp.edit();
            //如果点击记住密码
            if (bing.checkBox.isChecked()){
                editor.putString("isRem","1");
                editor.putString("userId",bing.getUserId());
                editor.putString("password",bing.getPassword());
                editor.commit();
            }else{
                editor.putString("userId",bing.getUserId());
                editor.putString("password",bing.getPassword());
                editor.putString("isRem","0");
                editor.commit();
            }
            Toast.makeText(getApplicationContext(), "登录成功！",
                    Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
            intent.putExtra("userId",bing.getUserId());
            startActivity(intent);
        }else{
            Toast.makeText(getApplicationContext(), "登录失败！",
                    Toast.LENGTH_SHORT).show();
        }
    }

    public void register_click(View view){
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(intent);
    }

    //判断是否存在用户
    public boolean isExist(){
        boolean rid = false; // 插入数据
        // 获得数据库对象
        SQLiteDatabase db = myUserHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        Cursor c = db.query(MyUserHelper.TABLE_NAME, new String[] {
                        MyUserHelper.ID, MyUserHelper.PWD, MyUserHelper.UserNAME,},
                MyUserHelper.ID + "=? and " + MyUserHelper.PWD + "=?",
                new String[] { bing.getUserId(), bing.getPassword() }, null, null, null);
        if (c.getCount() > 0) {
            rid = true;
        }
        db.close();
        return rid;
    }
}